#Homework 1, Problem 1

pval = 0.05

CompareGroups <- function(pval) {
	#Create two identical groups
  control.group1 <- rnorm(20, 0, 1)
  control.group2 <- c(control.group1, rnorm(10,0,1))
  exp.group1 <-rnorm(20,0,1)
  exp.group2 <-c(exp.group1,rnorm(10,0,1))
  
  
  #Two-Sample t-tests
  t.test1 <- t.test(control.group1, exp.group1, var.equal = TRUE) 
  t.test2 <- t.test(control.group2, exp.group2, var.equal = TRUE) 
  
  #signif = 1 if at least one test gives p < pval, 0 otherwise
  signif <- ifelse(t.test1$p.value < pval | #Vertical bar is logical "or"
  								 	t.test2$p.value < pval,
  								 1, 0)
  return(signif) #value returned by the function  
  
}

output <- replicate(15000, CompareGroups(.05))
avg.diff <- mean(output)
#The average percentage of rejections is...
avg.diff
